package com.example.asus.yuejian_10.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.asus.yuejian_10.Config;
import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.RetrofitSeveice;
import com.example.asus.yuejian_10.adapter.ClublistAdapter;
import com.example.asus.yuejian_10.base.BaseFragment;
import com.example.asus.yuejian_10.bean.ClubList;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * A simple {@link Fragment} subclass.
 */
public class ClubFragment extends BaseFragment {
    private ClublistAdapter adapter;
    private RecyclerView rv;
    private RetrofitSeveice service;



    public ClubFragment() {
        Retrofit retrofit =
                new Retrofit.Builder()
                        .baseUrl(Config.URL)
                        .addConverterFactory(GsonConverterFactory.create()).build();
         service = retrofit.create(RetrofitSeveice.class);

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        rv = (RecyclerView) view.findViewById(R.id.clublist);

        Call<List<ClubList>> call = service.clublist();

        call.enqueue(new Callback<List<ClubList>>() {
            @Override
            public void onResponse(Call<List<ClubList>> call, Response<List<ClubList>> response) {
                if(getActivity()!=null){

                    List<ClubList> clublist = response.body();

                    adapter = new ClublistAdapter(getActivity(),clublist);

                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());

                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

                    rv.setLayoutManager(linearLayoutManager);
                    rv.setAdapter(adapter);
                    rv.setItemAnimator(new DefaultItemAnimator());
                }
            }

            @Override
            public void onFailure(Call<List<ClubList>> call, Throwable t) {

            }
        });

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_club, container, false);

    }

}
